package com.nchu.learn.netty.resolver;

import com.nchu.learn.netty.enums.MessageTypeEnum;
import com.nchu.learn.netty.model.Message;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author fujianjian
 * @project self-learning
 * @date 2020/1/16 11:33
 */
@Slf4j
public class PingMessageResolver implements Resolver {
    private static final AtomicInteger counter = new AtomicInteger(1);

    @Override
    public Message resolve(Message message) {
        // 接收到消息进行处理，demo只是做打印处理
        int index = counter.getAndIncrement();
        log.info("[trx: {}] {} . receive ping message:{}",
                message.getSessionId(), index, message.getBody());


        // 处理完成后生成一个响应消息返回
        Message response = new Message();
        response.setMessageType(MessageTypeEnum.PONG);
        return response;
    }

    @Override
    public boolean support(Message message) {
        return message.getMessageType() == MessageTypeEnum.PING;
    }
}
